// pages/order/order.js
import request from '../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    active: 1,
    tabs: [{
        type: 1,
        name: '全部',
        load: false,
        list: []
      },
      {
        type: 2,
        name: '代付款',
        load: false,
        list: []
      },
      {
        type: 3,
        name: '待收货',
        load: false,
        list: []
      },
      {
        type: 4,
        name: '退款/退货',
        load: false,
        list: []
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onLoad(options) {
      this.setData({
        active: +options.type
      })
      this.getOrders(+options.type || 1)
    },
    async getOrders(type) {
      if (this.data.tabs[type - 1].load) {
        return
      }
      const res = await request({
        url: '/my/orders/all',
        data: {
          type
        }
      })
      this.setData({
        [`tabs[${type - 1}].list`]: res.data.message.orders.map(v => ({
          ...v,
          create_time_cn: (new Date(v.create_time * 1000).toLocaleString())
        })),
        [`tabs[${type - 1}].load`]: true
      })
    },
    onTabChange(e) {
      this.getOrders(e.detail.name)
      this.setData({
        active: e.detail.name
      })
    }
  }
})